<?php
/**
 * 
 * 
 * @version $Id: Sina.php 10 2012-10-11 08:50:50Z linsir123 $
 */
class modelSina 
{
	/**
	 * @var 模型
	 */
	public $weibo;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$this->weibo = Weibo::sina();
	}
	
	/**
	 * 取得关注[僵尸]
	 */
	public function getFriendsZombie()
	{
		$idsAll = $idsClean = $idsZombie = array();
		
		/// 正常
		$cursor = 0;
		do {
			$tmp = $this->weibo->getFriends(0, 100, $cursor);
			if (isset($tmp['users']) && is_array($tmp['users'])) {
				foreach ($tmp['users'] as $v) 
					$idsClean[] = $v['id'];
			}
			
			$cursor = intval($tmp['next_cursor']);
		} while ($cursor > 0);
		
		/// 全部
		$cursor = 0;
		do {
			$tmp = $this->weibo->getFriendsIds(0, 100, $cursor);
			if (isset($tmp['ids']) && is_array($tmp['ids'])) 
				$idsAll = array_merge($idsAll, $tmp['ids']);
				
			$cursor = intval($tmp['next_cursor']);
		} while ($cursor > 0);
		
		/// 僵尸
		$idsZombie = array_diff($idsAll, $idsClean);
		
		///
		return $idsZombie;
	}
	
	/**
	 * 取得关注
	 * 
	 * @param int p
	 */
	public function getFriends()
	{
		$show = 100;
		$p = intval($_POST['p']);
		if ($p <=0) 
			$p = 1;
			
		///
		$list = array();
		$cursor = ($p-1)*$show;
		$tmp = $this->weibo->getFriends(0, 100, $cursor);
		if (isset($tmp['users']) && is_array($tmp['users'])) {
			foreach ($tmp['users'] as $v) 
				$list[] = $this->_format($v);
		}
		
		///
		return array(
			'list' => $list,
			'end' => intval($tmp['next_cursor'])==0?1:0
		);
	}
	
	/**
	 * 取消关注
	 * 
	 * @param array uIds 
	 */
	public function destroy()
	{
		$uIds = (isset($_POST['uIds']) 
			&& is_array($_POST['uIds'])) ? $_POST['uIds'] : array();
			
		///
		if ( ! empty($uIds)) {
			foreach ($uIds as $uId) 
				$this->weibo->destroy($uId);
				
			return array(10000);
		}
		
		return array(10001, '请选择取消关注的用户');
	}
	
	
	///
	
	
	/**
	 * 检测某个用户是否符合查询标准
	 */
	private function _format($v)
	{
		///
		$keys = array(
			'follow_me', 			// 该用户是否关注当前登录用户
			'verified', 			// 是否是微博认证用户，即带V用户
			'gender', 				// 性别，m：男、f：女、n：未知
			'profile_image_url',	// 用户头像地址
			'followers_count',		// 粉丝数
			'friends_count',		// 关注数
		);
		
		///
		$row = array(
			'screen_name' => $v['screen_name'],
			'followers_count' => $v['followers_count'],
			'friends_count' => $v['friends_count'],
			'statuses_count' => $v['statuses_count'],
			'profile_image_url' => $v['profile_image_url'],
		);
		$tmp = explode('/', $v['profile_image_url']);
		$row['_hasPic'] = $tmp[5]!=0?1:0;
		$row['_fllowMe'] = $v['follow_me']?1:0;
		$row['_verified'] = $v['verified']?1:0;
		if ( ! empty($v['status']['created_at'])) {
			$lastTime = strtotime($v['status']['created_at']);
			$row['_lastPostTime'] = ceil((time()-$lastTime)/86400);
		} else 
			$row['_lastPostTime'];
			
		if ($v['gender']=='f') 
			$row['_gender'] = 0;
		else 
			$row['_gender'] = 1;
			
		///
		return $row;
	}
}
